package graphroute

import (
	"fmt"
	"power/core/common/constant"
	"power/core/dao/graphdao"
	"power/core/localwrap"
	"power/core/service/graphs"
	"power/core/service/users"
	"power/http/routes"

	"github.com/RocksonZeta/irisx"
	"github.com/kataras/iris/v12"
)

var logProjectUsers = localwrap.GetLogger().Fork("power/http/graphroute", "RouteProjectUsers")

//RouteProjectUsers /api/project
func RouteProjectUsers(party iris.Party) {
	party.Get("/{id:int}", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		id, err := c.Params().GetInt("id")
		fmt.Println(err)
		uid := c.GetUidInt()
		logProjectUsers.Debug().Str("path", c.Path()).Int("id", id).Int("uid", uid).Send()
		r := graphs.ProjectUsers{}.Get(id, uid)
		c.Ok(r)
	})
	party.Get("/list", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		var param graphdao.ProjectUsersQueryParam
		param.Ps = 20
		param.Pi = c.CheckQuery("p").Int(1) - 1
		param.Name = c.CheckQuery("name").String()
		routes.Check(c)
		uid := c.GetUidInt()
		logProjectUsers.Debug().Str("path", c.Path()).Int("uid", uid).Send()
		u := users.Users{}.Get(uid)
		if u.Role != constant.RoleAdmin {
			param.Uid = uid
		}
		r, total := graphs.ProjectUsers{}.Query(param)
		routes.Page(c, r, total, 20, param.Pi)
	})
	party.Post("/", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		var param graphs.ProjectUsersAddParam
		param.Name = c.CheckBody("name").NotBlank().String()
		param.BidDate = routes.CheckBodyTimeNull(c, "bidDate", "")
		param.CompletionDate = routes.CheckBodyTimeNull(c, "completionDate", "")
		param.ManagerPhone = routes.CheckBodyStringNull(c, "managerPhone")
		param.Manager = routes.CheckBodyStringNull(c, "manager")
		param.MapCountry = c.CheckBody("mapCountry").Empty().String()
		param.ProjectAddr = routes.CheckBodyStringNull(c, "projectAddr")
		param.ProjectType = routes.CheckBodyIntNull(c, "projectType")
		param.Remark = routes.CheckBodyStringNull(c, "remark")
		param.StaffNum = routes.CheckBodyIntNull(c, "staffNum")
		param.Uid = c.GetUidInt()
		// param.User.Uid = c.GetUidInt()
		// param.Name = c.CheckBody("name").NotBlank().String()
		// logProjectUsers.Debug().Str("path", c.Path()).Interface("param", param).Send()
		routes.Check(c)
		graphs.ProjectUsers{}.Add(param)
		c.Ok(nil)
	})
	party.Post("/update", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		var param graphdao.ProjectUsersUpdateParam
		param.Id = c.CheckBody("id").NotBlank().Int(0)
		// param.Name = c.CheckBody("name").NotBlank().String()
		logProjectUsers.Debug().Str("path", c.Path()).Interface("param", param).Send()
		routes.Check(c)
		graphs.ProjectUsers{}.Update(param)
		c.Ok(nil)
	})
	party.Post("/delete", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		id := c.CheckBody("id").NotBlank().Int(0)
		logProjectUsers.Debug().Str("path", c.Path()).Int("uid", c.GetUidInt()).Send()
		routes.Check(c)
		graphs.ProjectUsers{}.Delete(id)
		c.Ok(nil)
	})
}
